Chapter 4 LOGICAL COMPARES AND PRECEDENCE WHAT IS A BOOLEAN VARIABLE? _________________________________________________________________ Examine the program named COMPARE.ADA for an =============== example of logical compares being used in a very COMPARE.ADA trivial way. We declare and initialize three =============== INTEGER type variables for use later then declare two variables of type BOOLEAN in lines 14 and 15, with the first one being initialized to TRUE. A BOOLEAN type variable has a very limited range of values which can be assigned to it, namely TRUE or FALSE, and there are no mathematical operations available for use on variables of type BOOLEAN. Much more will be said about the BOOLEAN type variable later in this tutorial, but we need a basic understanding of a boolean variable in order to study program control in the next chapter. Lines 19 and 23 illustrate how you can assign a value of either TRUE or FALSE to a BOOLEAN variable. These illustrate literal assignment in much the same way that a literal value can be assigned to an INTEGER type variable. Because we wish to display BOOLEAN values, we instantiate a copy of the generic package Enumeration_IO for the BOOLEAN type. Once again, we will study the details of this later in the tutorial. This package makes it possible to output BOOLEAN values in lines 21 and 25. BOOLEAN ASSIGNMENT STATEMENTS _________________________________________________________________ Lines 28 through 30 are a bit more interesting because they illustrate assigning a calculated BOOLEAN value to a BOOLEAN variable. In line 28, the value of the INTEGER variable One has the literal 1 added to it and the total is compared to the value contained in the variable Two. The expression reduces to the expression 1 + 1 = 2, and since 1 + 1 is equal to 2, the expression evaluates to TRUE which is assigned to the BOOLEAN variable Is_It. The various steps in evaluation are illustrated below for the student with little or no experience using BOOLEAN expressions in some other programming language. Is_It := (One + 1) = Two; Is_It := (1 + 1) = 2; Is_It := 2 = 2; Is_It := TRUE; The single equal sign is the BOOLEAN operator for equality, and if the two expressions being evaluated are of the same value, a value Page 4-1 Chapter 4 - Logical Compares and Precedence of TRUE will result. If they are not of the same value, the BOOLEAN result will be FALSE. ARE THERE OTHER BOOLEAN OPERATORS? _________________________________________________________________ There are a total of six BOOLEAN operators, and all six will be illustrated in the next example program. Line 29 in the present program illustrates use of one of the others, the inequality operator. This statement says, if One is not equal to Two then assign the value of TRUE to the BOOLEAN variable Is_It, otherwise assign a value of FALSE to the BOOLEAN variable Is_It. Note that regardless of the result, a value will be assigned to the BOOLEAN variable. Finally, the "greater than or equal" operator is illustrated in use in line 30. This is a rather silly program since none of the results are used, but it is meant to illustrate just what a BOOLEAN variable is and how to use it. Compile and run this program before continuing on to the next example program. ADDITIONAL TOPICS ON BOOLEAN EVALUATION _________________________________________________________________ Examine the program named COMPARES.ADA and you ================ will see all six BOOLEAN operators in use. We COMPARES.ADA define two INTEGER and three BOOLEAN type ================ variables in the declaration part of the program, and begin the executable part with some compare examples in lines 17 and 18. It should be clear that in line 18, Index can not be less than itself, so the result is FALSE. Lines 21 through 26 illustrate the use of all six BOOLEAN operators which are available in Ada and you should have no trouble understanding this list. THE BOOLEAN and OPERATOR _________________________________________________________________ Lines 29 through 32 illustrate composite BOOLEAN operators using the reserved words and, or, not, and xor. The statement in line 29 says that if Index = 12 and if Count = 12 and if Truth currently has the value TRUE and if TRUE (which is always TRUE) then assign TRUE to Question otherwise assign FALSE to Question. Page 4-2 Chapter 4 - Logical Compares and Precedence Using the freeform available in an Ada program to write the previous sentence, can help in understanding the sentence. The point to be illustrated is that you can combine as many BOOLEAN expressions as you desire to do a particular job. THE BOOLEAN or OPERATOR _________________________________________________________________ Using the expanded freeform available in Ada, the statement in line 30 says that if Index is not equal to 12 or if FALSE (which is always FALSE) or if Count is greater than 3 or if Truth currently has the value of TRUE then assign TRUE to Question otherwise assign FALSE to Question. THE BOOLEAN not OPERATOR _________________________________________________________________ The expression in line 31 illustrates the use of these two operators combined in a slightly more complex way using parentheses to properly group the expressions. A new operator appears here, the not which simply says to reverse the meaning of the BOOLEAN operator which it precedes. THE BOOLEAN xor OPERATOR _________________________________________________________________ Line 32 illustrates the use of the "exclusive or" operator which says that the result will be TRUE if one and only one of the operands are TRUE, and FALSE if both operands are TRUE, or both operands are FALSE. A good illustration of this operation would be a hall light with a switch at both ends of the hall. If one of the switches is up, the light is on, but if both are up or both are down, the light is off. FULL EVALUATION OF BOOLEAN EXPRESSIONS _________________________________________________________________ The way the expressions are written in lines 29 through 32, all of the expressions will be evaluated when the statement is executed. If, in the case of line 29, the value of Index is not 12, then the final result will be FALSE no matter what the rest of the expressions are and it would be a waste of time to evaluate them. Page 4-3 Chapter 4 - Logical Compares and Precedence Ada will continue blindly across the entire line evaluating all of the expressions and wasting time since it should know the final result based on the first comparison. There is a way to tell it to stop as soon as it knows the final answer, through use of the short circuit operators. WHAT ARE "SHORT CIRCUIT OPERATORS"? _________________________________________________________________ If you study line 35, you will see that if Index is equal to Count, we will be dividing the constant 9 by zero in the second part of the expression. By adding the reserved word then to the and operator we have a short circuit operation, which means as soon as the system knows the final outcome, the remaining operations are short circuited and not executed. In the present example, if Index is equal to Count, the first term is FALSE and there is no need to continue since the second term is to be anded with the FALSE resulting in FALSE no matter what the second term is. Division by zero is avoided in this case because the division is not attempted. In the same manner, line 36 illustrates the short circuit or operator which is obtained by adding the reserved word else. In this case, if Index is equal to Count, the result will be TRUE regardless of what the second term is, so the second term is not evaluated and division by zero is avoided. Line 37 is identical to line 36 but illustrates the use of parentheses to make the logic a little easier to read. ORDER OF PRECEDENCE _________________________________________________________________ The order of precedence of operators is given by the following list. ** not abs -- Highest precedence * / mod rem -- Multiplying operators + - -- Unary operators + - & -- Binary adding operators = /= < -- Relational operators <= > >= -- Relational operators in not in -- (same precedence) and or xor -- Logical operators and then or else -- (same precedence) The LRM has a complete list of the operators and the details of the order of precedence in section 4.5. If there is any question as to the order of precedence, you should group expressions together with parentheses since they have the absolute highest precedence. A future reader of your program will know exactly what your program is doing. Page 4-4 Chapter 4 - Logical Compares and Precedence Be sure to compile and execute this program. Note that we have not yet studied the &, the in, and the not in operators but will soon. PROGRAMMING EXERCISE _________________________________________________________________ 1. Add some output statements to both example programs to see that the results are as predicted. This will give you experience using the boolean output statement. Page 4-5